<template>
  <div class="container">
    <div class="formBox">
      <div class="formTitle">南浔区餐饮油烟智慧监管平台</div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="login_form"
      >
        <el-form-item prop="username">
          <div class="inputTitle">账号：</div>
          <el-input
            class="elInputStyle"
            type=""
            placeholder="请输入账号"
            v-model.number="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <div class="inputTitle">密码：</div>
          <el-input
            class="elInputStyle"
            type="password"
            placeholder="请输入密码"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="inputCode">
          <div class="inputTitle">校验码：</div>
          <div class="jiaoyanmaInp">
            <el-input
              class="elInputStyle"
              placeholder="请输入校验码"
              v-model="ruleForm.inputCode"
              autocomplete="off"
            ></el-input>
            <div class="loginImg">
              <img id="captchaImage" alt="" @click="chooseImg"/>
            </div>
          </div>
        </el-form-item>
        <el-button class="submitBtn" type="primary" @click="submitForm()"
          >登录</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { newLogin, addUserLogin, captcha } from '@/api/index'
export default {
  name: 'loginPage',
  props: {},
  components: {},
  data() {
    return {
      ruleForm: {
        username: '',
        password: '',
        inputCode: '',
      },
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [
          { required: true, message: '请输入密码' },
          { min: 8, message: '密码长度大于8个字符', trigger: 'blur' },
          {
            pattern:
              /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[`~!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]).{8,}$/,
            message: '密码必须包含大小写字母、数字和至少一个特殊符号',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    async submitForm() {
      const res = await newLogin({
        username: this.ruleForm.username,
        password: this.ruleForm.password,
        inputCode: this.ruleForm.inputCode,
      })
      if (res.code == 0) {
        this.$store.commit('storeToken', res.data)
        const name = this.ruleForm.username
        const psd = btoa(this.ruleForm.password)

        const res2 = await addUserLogin({
          id: res.data.userId,
          username: name,
          password: psd,
        })
        if (res2.code == 0) {
          localStorage.setItem('nanxun_id', res2.data.id)
          this.$router.push({
            path: 'index',
            query: {
              identity: res2.data.id,
            },
          })
        }
      }
    },
    async getCaptcha() {
      const response = await captcha()
      // 获取响应数据，它是 Blob 对象
      const blob = response.data
      // 创建一个指向该 Blob 对象的 URL
      const imageUrl = URL.createObjectURL(blob)

      // 将 URL 赋值给 img 标签的 src 属性
      document.getElementById('captchaImage').src = imageUrl
    },
    chooseImg() {
      this.getCaptcha()
    }
  },
  created() {
    this.getCaptcha()
  },
  mounted() {},
  watch: {},
  computed: {},
  filters: {},
}
</script>

<style scoped lang="scss">
.container {
  width: 1920px;
  height: 1080px;
  background-image: url('@/assets/花瓣素材_等距网络机器人与网络安全、人工智能概念。ChatBot免费的机器人虚拟协助网站或移动应用程序。矢量图_117346293-[转换]@1x.png');
  .formBox {
    position: absolute;
    left: 140px;
    top: 250px;
    .formTitle {
      letter-spacing: 0.1em;
      margin-bottom: 20px;
      font-size: 40px;
      font-family: Alimama ShuHeiTi;
      color: #021770;
      font-weight: 800;
    }
    .elInputStyle {
      width: 447px;
      height: 50px;
    }
    ::v-deep .el-input__inner {
      background-color: #ededed !important;
      height: 50px;
      line-height: 50px;
      width: 447px;
      border-radius: 6px;
    }
    ::v-deep .el-input__inner::-webkit-input-placeholder {
      /* WebKit browsers，webkit内核浏览器 */
      color: #b3b3b3;
      letter-spacing: 0.3em;
      font-size: 20px;
      border-radius: 6px;
    }
    ::v-deep .el-input__inner:-moz-placeholder {
      /* Mozilla Firefox 4 to 18 */
      color: #b3b3b3;
      letter-spacing: 0.2em;
      font-size: 20px;
      border-radius: 6px;
    }
    ::v-deep .el-input__inner::-moz-placeholder {
      /* Mozilla Firefox 19+ */
      color: #b3b3b3;
      letter-spacing: 0.2em;
      font-size: 20px;
      border-radius: 6px;
    }
    ::v-deep .el-input__inner:-ms-input-placeholder {
      /* Internet Explorer 10+ */
      color: #b3b3b3;
      letter-spacing: 0.2em;
      font-size: 20px;
      border-radius: 6px;
    }

    .el-form-item {
      margin-bottom: 10px;
    }
    .inputTitle {
      font-weight: 800;
      font-size: 20px;
      color: #000;
      margin-bottom: 16px;
      letter-spacing: 0.3em;
      font-family: Source Han Sans;
    }
    .submitBtn {
      width: 447px;
      height: 50px;
      text-align: center;
      font-size: 22px;
      font-weight: 800;
      letter-spacing: 0.4em;
      line-height: 50px;
      background-color: #448be3;
      padding: 0;
      margin-top: 40px;
      border-radius: 6px;
    }
    .jiaoyanmaInp {
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 447px;
      .loginImg {
        width: 200px;
        height: 50px;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .elInputStyle {
        width: 220px;
        height: 50px;
      }
      ::v-deep .el-input__inner {
        background-color: #ededed !important;
        height: 50px;
        line-height: 50px;
        width: 220px;
        border-radius: 6px;
      }
    }
  }
}
</style>
